package com.citcc.web.controller.business;

import com.citcc.business.bean.CdzManufacturer;
import com.citcc.business.bean.CdzStation;
import com.citcc.business.service.CdzManufacturerService;
import com.citcc.business.service.CdzStakeService;
import com.citcc.common.annotation.Log;
import com.citcc.common.base.AjaxResult;
import com.citcc.common.enums.BusinessType;
import com.citcc.common.page.TableDataInfo;
import com.citcc.framework.util.ShiroUtils;
import com.citcc.framework.web.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/cdzManufacturer")
public class CdzManufacturerController extends BaseController {

    private String prefix = "business/cdzManufacturer";

    @Autowired
    private CdzManufacturerService manufacturerService;
    @Autowired
    private CdzStakeService cdzStakeService;

    @RequiresPermissions("business:cdzManufacturer:view")
    @GetMapping("/main")
    public String vehicle(){
        return prefix + "/main";
    }

    @RequiresPermissions("business:cdzManufacturer:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(CdzManufacturer manufacturer){
        startPage();
        if (manufacturer.getOrgId()==null||"".equals(manufacturer.getOrgId())) {
            manufacturer.setOrgId(getSysUser().getDeptId());
        }
        List<CdzManufacturer> list=manufacturerService.selectAllList(manufacturer);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String toAdd()
    {
        return prefix + "/add";
    }

    @RequiresPermissions("business:cdzManufacturer:add")
    @Log(title = "厂商管理", businessType = BusinessType.INSERT)
    @PostMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave(CdzManufacturer manufacturer)
    {
        if (manufacturer.getOrgId()==null||"".equals(manufacturer.getOrgId())) {
            manufacturer.setOrgId(getSysUser().getDeptId());
        }
        manufacturer.setCreateBy(ShiroUtils.getLoginName());
        manufacturer.setDelFlag("0");
        return toAjax(manufacturerService.add(manufacturer));
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id")String id, ModelMap map)
    {
        map.put("manufacturer",manufacturerService.getManufacturer(id));
        return prefix + "/edit";
    }


    @RequiresPermissions("business:cdzManufacturer:edit")
    @Log(title = "厂商管理", businessType = BusinessType.UPDATE)
    @PostMapping("/editSave")
    @ResponseBody
    public AjaxResult editSave(CdzManufacturer manufacturer)
    {
        manufacturer.setUpdateBy(ShiroUtils.getLoginName());
        manufacturer.setUpdateTime(new Date());
        return toAjax(manufacturerService.uodate(manufacturer));
    }

    @RequiresPermissions("business:cdzManufacturer:remove")
    @Log(title = "厂商管理", businessType = BusinessType.UPDATE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String id)
    {
        try
        {
            int count=cdzStakeService.countManufacturer(id);
            if(count>0){
                return error("该厂商绑定充电桩不能删除");
            }else{
                Integer e =manufacturerService.delete(id);
                return toAjax(e);
            }

        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }


}
